package com.liujit.standard.basis.admin.config.event;

import com.alibaba.fastjson.JSON;
import com.liujit.standard.basis.admin.model.SysLog;
import com.liujit.standard.basis.admin.service.SysLogService;
import com.liujit.standard.basis.core.util.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j(topic = "operateLog")
@Component
public class SysLogListener {

    @Resource
    private SysLogService sysLogService;

    @Async
    @Order
    @EventListener(SysLogEvent.class)
    public void saveSysLog(SysLogEvent event) {
        SysLog sysLog = (SysLog) event.getSource();
        sysLog.setId(IdGenerator.getId());
        log.info(JSON.toJSONString(sysLog));
        // 保存日志
        sysLogService.save(sysLog);
    }
}
